<template>
  <view class="page">
    <view class="title">欢迎使用车迹数管理系统</view>
    <view style="width: 100%; height: 300rpx"></view>
    <view class="item" >
      <view class="item_1">账号/手机</view>
      <view class="inputBox"
        ><input type="text" placeholder="请输入账号/手机" v-model="account"
      /></view>
    </view>
    <view class="item" v-if="is_role == '否'">
      <view class="item_1">密码</view>
      <view class="inputBox"
        ><input type="password" placeholder="请输入密码" v-model="password"
      /></view>
    </view>
    <!-- <view class="hint" style="padding-left: 30rpx;">注：为装卸人员时,仅需输商户号即可</view> -->
    <view class="btn" @click="toLogin(1)">{{
      is_role != "否" ? "一键绑定" : "登录"
    }}</view>
  </view>
</template>

<script>
import { getUserInfo, userLogin } from "@/api/user.js";
import setting from "./../../common/config";
import http from "@/common/uni.interface.js";
export default {
  data() {
    return {
      account: "",
      password: "",
      code: "",
      is_role: "否",
      show: false,
    };
  },
  onLoad(option) {
    console.log(option);
    switch (option.isType) {
      case "sh":
        uni.setStorageSync("isType", "sh");
        this.is_role = "否";
        break;
      case "cz":
        uni.setStorageSync("isType", "cz");
        this.is_role = "是";
        break;
      case "zx":
        uni.setStorageSync("isType", "zx");
        this.is_role = "是";
        break;
    }
    if (
      option.isType == undefined ||
      option.isType == "" ||
      option.isType == null
    ) {
      let isType = uni.getStorageSync("isType");
      switch (isType) {
        case "sh":
          uni.setStorageSync("isType", "sh");
          this.is_role = "否";
          break;
        case "cz":
          uni.setStorageSync("isType", "cz");
          this.is_role = "是";
          break;
        case "zx":
          uni.setStorageSync("isType", "zx");
          this.is_role = "是";
          break;
      }
    }
    if (option.code) {
      this.code = option.code;
      if (uni.getStorageInfoSync("isType") == "cz") {
        uni.navigateTo({ url: "/pages/czCatList/index" });
      }
    } else {
      this.wxAuthorize();
    }
  },
  methods: {
    toLogin(index) {
      if (index == 1) {
        if (this.is_role == "否") {
          userLogin({
            account: this.account,
            password: this.password,
            code: this.code,
            is_role: uni.getStorageSync("isType") != "sh" ? 1 : 0,
          }).then((res) => {
            console.log(res.data);
            http.config.header.token = res.data.userinfo.token;
            uni.setStorageSync("token", res.data.userinfo.token);
            uni.setStorageSync("userType", res.data.userinfo.type);
            switch (res.data.userinfo.type) {
              case 5:
                //  厂家
                uni.reLaunch({ url: "/pages/manufacturerPage/index" });
                break;
              case 4:
                //  商户
                uni.reLaunch({ url: "/pages/manufacturer/index" });
                break;
              case 3:
                //  车队队长
                uni.reLaunch({ url: "/pages/motorcade/index" });
                break;
              case 2:
                //  运输车车主
                uni.reLaunch({ url: "/pages/owner/index" });
                break;
              case 1:
                // uni.reLaunch({ url: '/pages/owner/index' })
                break;
            }
          });
        }else {
         if(uni.getStorageSync('isType') == 'cz'){
			  uni.navigateTo({ url: "/pages/czCatList/index?code=" + this.code + '&phone=' + this.account });
		 }else {
			 uni.navigateTo({ url: "/pages/zxList/zxList?code=" + this.code + '&phone=' + this.account });
		 }
		}
      }
    },
    getCode() {
      var url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx54a8bdef5a94e7a8&redirect_uri=https://counter.flygeneral.top/index.html&&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
      window.location.href = url + 'isType=' + uni.getStorageSync('isType');
    },
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      let url = window.location.href.split("#")[0];
      let search = url.split("?")[1];
      if (search) {
        var r = search.substr(0).match(reg);
        if (r !== null) return unescape(r[2]);
        return null;
      } else {
        return null;
      }
    },
    wxAuthorize() {
      let link = window.location.href;
      // let params = this._getUrlParams(link);  // 地址解析
      //console.log(link);

      let params = this.getUrlParam("code"); // 地址解析
      console.log(params);
      // 已经授权登录过的就不用再授权了
      // if (store.state.token) return;

      // 如果拿到code，调用授权接口，没有拿到就跳转微信授权链接获取
      // if (params.code) {
      if (params) {
        this.code = params;
      } else {
        this.getCode();
      }
    },
  },
};
</script>

<style lang="scss">
.page {
  width: 100%;
  height: 100vh;
  // background-color: #f5f5f5;

  .title {
    width: 94%;
    margin: auto;
    padding-top: 100rpx;
    font-size: 46rpx;
    font-weight: 600;
    // margin-top: 200rpx;
  }

  .item {
    display: flex;
    width: 90%;
    margin: auto;
    height: 80rpx;
    line-height: 80rpx;
    margin-top: 20rpx;
    border-bottom: 1px solid #efefef;

    view:nth-child(1) {
      width: 25%;
    }

    view:nth-child(2) {
      width: 75%;
      text-align: right;

      input {
        width: 100%;
        height: 100%;
      }
    }
  }

  .btn {
    background: $uni-color-primary;
    border-radius: 8px 8px 8px 8px;
    width: 90%;
    height: 80rpx;
    margin: auto;
    margin-top: 120rpx;
    color: #fff;
    text-align: center;
    line-height: 80rpx;
  }
}

.items {
  display: flex;
  width: 94%;
  height: 80rpx;
  margin: auto;
  line-height: 80rpx;
  font-size: 28rpx;
  border-bottom: 1px solid #efefef;

  .titles {
    width: 70%;
  }

  .value {
    margin-top: 20rpx;
  }
}
</style>
